home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / umich / tex / tex31 / texsrc.lzh / COMMON.LZH / FILEIO.C < prev    next >
Encoding:
C/C++ Source or Header  |  1991-01-13  |  4.3 KB  |  163 lines

  1. /* fileio.c: routines used by TeX, Metafont, and BibTeX.  */
  2.  
  3. #include "../common/fileio.h"
  4.  
  5. #ifndef BibTeX
  6. #define EXTERN /* Don't instantiate date here.  */
  7. #ifdef TeX
  8. #include "texd.h"
  9. #else
  10. #include "mfd.h"
  11. #endif
  12. #endif /* not BibTeX */
  13.  
  14. /* This is defined in ./texmf.c.  */
  15. extern void funny_core_dump ();
  16.  
  17. #ifdef TeX
  18. /* See comments in ctex.ch for why we need this.  */
  19. extern integer tfmtemp;
  20. #endif
  21.  
  22.  
  23. #ifdef BibTeX
  24. /* See comments in bibtex.ch for why we need these.  */
  25. FILE *standardinput = stdin;
  26. FILE *standardoutput = stdout;
  27.  
  28. /* Because we don't generate a .h file with all the global definitions
  29.    for BibTeX, as we do with TeX and Metafont, we must declare all these
  30.    variables.  */
  31. extern char nameoffile[];
  32. extern integer namelength;
  33. #endif
  34.  
  35.  
  36.  
  37. /* This constant is used in BibTeX, when opening the top-level .aux file.  */
  38. #define NO_FILE_PATH -1
  39.  
  40. /* Open an input file F, using the path PATHSPEC (the values are defined
  41.    in ./extra.h).  The filename is in `nameoffile', as a Pascal string.
  42.    We return whether or not the open succeeded.  If it did, we also set
  43.    `namelength' to the length of the full pathname that we opened.  */
  44.  
  45. boolean
  46. open_input (f, path_index)
  47.   FILE **f;
  48.   int path_index;
  49. {
  50. #ifdef BSD
  51. #ifndef BibTeX
  52.   /* This only applies if a preloaded TeX (or Metafont) is being made;
  53.      it allows for automatic creation of the core dump (typing ^\
  54.      requires manual intervention).  */
  55.   if (path_index == TEXINPUTPATH
  56.       && strncmp (nameoffile+1, "HackyInputFileNameForCoreDump.tex", 33) == 0)
  57.     funny_core_dump ();
  58. #endif
  59. #endif /* BSD */
  60.  
  61. #ifdef BibTeX
  62.   if (path_index == NO_FILE_PATH)
  63.     {
  64.       unsigned temp_length;
  65.  
  66.       /* We can't use `make_c_string' or `make_pascal_string', since
  67.          `nameoffile' is an array, not a pointer.  */
  68.       end_with_null (nameoffile + 1);
  69.       *f = fopen (nameoffile + 1, "r");
  70.       temp_length = strlen (nameoffile + 1);
  71.       end_with_space (nameoffile + 1);
  72.       if (*f != NULL)
  73.         {
  74.           namelength = temp_length;
  75.           return true;
  76.         }
  77.       else
  78.         return false;
  79.     }
  80. #endif
  81.  
  82.   if (testreadaccess (nameoffile, path_index))
  83.     {
  84.       *f = checked_fopen (nameoffile, "r");
  85.       
  86.       /* If we found the file in the current directory, don't leave the
  87.          `./' at the beginning of `nameoffile', since it looks dumb when
  88.          TeX says `(./foo.tex ... )', and analogously for Metafont.  */
  89.       if (nameoffile[1] == '.' && (nameoffile[2] == '/'
  90. #ifdef atarist
  91.                    || nameoffile[2] == '\\'
  92. #endif
  93.                    ))
  94.         {
  95.           unsigned i;
  96.           for (i = 1; nameoffile[i]; i++)
  97.             nameoffile[i] = nameoffile[i+2];
  98.         }
  99.  
  100.       namelength = strlen (nameoffile + 1);
  101.       
  102.       /* `nameoffile' is in an anomalous state: it still begins with a
  103.          space, but now it is terminated with a null.  */
  104.       end_with_space (nameoffile);
  105.       
  106. #ifdef TeX
  107.       /* If we just opened a TFM file, we have to read the first byte,
  108.          since TeX wants to look at it.  */
  109.       if (path_index == TFMFILEPATH)
  110.         tfmtemp = getc (*f);
  111. #endif
  112.  
  113.       return true;
  114.     }
  115.   else
  116.     return false;
  117. }
  118.  
  119.  
  120. /* Open an output file F either in the current directory or in
  121.    $TEXMFOUTPUT/F, if the environment variable `TEXMFOUTPUT' exists.
  122.    (Actually, this applies to the BibTeX output files, also, but
  123.    `TEXMFBIBOUTPUT' was just too long.)  The filename is in the global
  124.    `nameoffile', as a Pascal string.  We return whether or not the open
  125.    succeeded.  If it did, the global `namelength' is set to the length
  126.    of the actual filename.  */
  127.  
  128. boolean
  129. open_output (f)
  130.   FILE **f;
  131. {
  132.   unsigned temp_length;
  133.  
  134.   /* We can't use `checked_fopen' here, since that routine aborts if the
  135.      file can't be opened.  We also can't use `make_c_string' or
  136.      `make_pascal_string', since `nameoffile' is an array, not a
  137.      pointer.  */
  138.   end_with_null (nameoffile + 1);
  139.   *f = fopen (nameoffile + 1, "w");
  140.   
  141.   if (*f == NULL)
  142.     {
  143.       /* Can't open in the current directory.  Try the directory
  144.          specified by the environment variable.  */
  145.       char *temp_dir = getenv ("TEXMFOUTPUT");
  146.  
  147.       if (temp_dir != NULL && chdir (temp_dir) == 0)
  148.         *f = fopen (nameoffile + 1, "w");
  149.     }
  150.  
  151.   temp_length = strlen (nameoffile + 1);
  152.   end_with_space (nameoffile + 1);
  153.  
  154.   if (*f != NULL)
  155.     {
  156.       namelength = temp_length;
  157.       return true;
  158.     }
  159.   
  160.   else
  161.     return false;
  162. }
  163.